using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CardSystemUtils
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            cboCardType.DataSource = Enum.GetValues(typeof(CardType));
            cboCardType.SelectedIndex = 0;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            CardType type = (CardType)cboCardType.SelectedItem;
            byte[] digits = Generator.CreateCardNo(type);
            string rs = DigitsConverter.ByteArrayToString(digits);
            txtCardNo.Text = rs;
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            byte[] digits = DigitsConverter.StringToByteArray(txtCardNo.Text);
            bool rs = Validator.CheckLuhn(digits);
            MessageBox.Show(rs.ToString());
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            byte[] code = Encoder.EncryptSHA1(txtCardNo.Text);

            string path = Application.StartupPath + @"\Cn.Config";
            string CnStr = "";
            using (StreamReader rdr = new StreamReader(path))
            {
                CnStr = rdr.ReadLine();
            }

            string proc = "";
            CardType type = (CardType)cboCardType.SelectedItem;
            switch (type)
            {
                case CardType.Master:
                    proc = "sp_AddMasterCard";
                    break;
                case CardType.Visa:
                    proc = "sp_AddVisaCard";
                    break;
            }
            SqlConnection Cn = new SqlConnection(CnStr);
            SqlCommand cmd = new SqlCommand(proc, Cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@Code", SqlDbType.VarBinary);
            cmd.Parameters.Add("@Owner", SqlDbType.NVarChar);
            cmd.Parameters.Add("@ExpiredDate", SqlDbType.DateTime);

            cmd.Parameters["@Code"].Value = code;
            cmd.Parameters["@Owner"].Value = "Irvine Keaness";
            cmd.Parameters["@ExpiredDate"].Value = new DateTime(2011, 12, 31);

            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();

            MessageBox.Show("OK");
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            byte[] code = Encoder.EncryptSHA1(txtCardNo.Text);

            string path = Application.StartupPath + @"\Cn.Config";
            string CnStr = "";
            using (StreamReader rdr = new StreamReader(path))
            {
                CnStr = rdr.ReadLine();
            }

            string proc = "";
            CardType type = (CardType)cboCardType.SelectedItem;
            switch (type)
            {
                case CardType.Master:
                    proc = "sp_MasterCardInfo";
                    break;
                case CardType.Visa:
                    proc = "sp_VisaCardInfo";
                    break;
            }

            SqlConnection Cn = new SqlConnection(CnStr);
            SqlDataAdapter da = new SqlDataAdapter(proc, Cn);
            SqlCommand cmd = da.SelectCommand;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Code", SqlDbType.VarBinary);
            cmd.Parameters["@Code"].Value = code;

            DataTable dt = new DataTable();
            da.Fill(dt);

            if (dt.Rows.Count == 0) MessageBox.Show("Not found");
            else MessageBox.Show(dt.Rows[0]["Owner"].ToString());
        }
    }
}